<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <div slot="left">
        <img class="left-logo" src="@/assets/logo.png" />
      </div>
      <van-button
        class="search-btn"
        slot="right"
        icon="search"
        type="info"
        size="small"
        round
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- /导航栏 -->
    <!-- 频道列表 -->
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab
        :title="channel.name"
        v-for="channel in channels"
        :key="channel.id"
      >
        <!-- 频道的文章列表 -->
        <article-list :channels="channel" />
        <!-- /频道的文章列表 -->
      </van-tab>
      <!-- 占位用的 -->
      <div slot="nav-right" class="placeholder"></div>
      <!-- 汉堡按钮 -->
      <div
        slot="nav-right"
        class="hanburger-btn"
        @click="isChennelEditShow = true"
      >
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!-- /频道列表 -->

    <!-- 频道编辑弹出层 -->
    <van-popup
      v-model="isChennelEditShow"
      closeable
      position="bottom"
      close-icon-position="top-left"
      :style="{ height: '100%' }"
    >
      <channel-edit
        :my-channels="channels"
        :active="active"
        @update-active="onUpdateActive"
      />
    </van-popup>
    <!-- /频道编辑弹出层 -->
  </div>
</template>
<script>
import { getUserChannels } from '@/api/user'
import ArticleList from './components/article-list'
import ChannelEdit from './components/channel-edit'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'
export default {
  name: 'HomeIndex',
  components: { ArticleList, ChannelEdit },
  props: {},
  data() {
    return {
      active: 0,
      channels: [], // 频道列表
      isChennelEditShow: false // 控制编辑频道弹出层的显示状态
    }
  },
  computed: { ...mapState(['user']) },
  watch: {},
  methods: {
    // 本地存储
    async loadChannels() {
      try {
        // const { data } = await getUserChannels()
        // // console.log(data)
        // this.channels = data.data.channels

        let channels = []
        // 已登录，请求获取用户频道列
        if (this.user) {
          const { data } = await getUserChannels()
          channels = data.data.channels
        } else {
          // 未登录，判断是否有本地的频道列表数据
          const loadChannels = getItem('TOUTIAO_CHANNELS')
          // 有，拿来使用
          if (loadChannels) {
            channels = loadChannels
          } else {
            // 没有，请求获取默认频道列表
            const { data } = await getUserChannels()
            channels = data.data.channels
          }
        }
        this.channels = channels
      } catch (error) {
        this.$toast('获取频道数据失败！')
      }
    },
    onUpdateActive(index, isChennelEditShow = true) {
      // 更新激活的频道项
      this.active = index
      // 关闭编辑频道弹出层
      this.isChennelEditShow = isChennelEditShow
    }
  },
  created() {
    this.loadChannels()
  },
  mounted() {}
}
</script>
<style scoped lang="less">
.home-container {
  // 1./deep/  2.::deep  3.>>> 深度选择器，在vue中，我们为了避免父组件的样式影响
  // 到子组件的样式，会在style中加/deep/，这样父组件
  // 中如果有跟子组件相同的class名称或者使用选择器的时候，就不
  // 会影响到子组件的样式。
  // 此处我们使用第三种，但是要先声明一下,/deep/ 在PC或者不在less写法中使用
  padding-bottom: 100px;
  padding-top: 174px;
  @deep: ~'>>>';
  @{deep}.van-nav-bar__title {
    // 使用的时候：@{变量名称}
    max-width: unset;
  }
  .van-nav-bar__content {
    display: flex;
    align-items: center;
    .left-logo {
      margin-top: 10px;
      width: 265px;
      height: 85px;
    }
    .search-btn {
      width: 400px;
      height: 64px;
      background-color: #5babfb;
      border: none;
      font-size: 28px;
      .van-icon {
        font-size: 32px;
      }
    }
  }

  @{deep}.channel-tabs {
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
      height: 82px;
    }
    .van-tab {
      min-width: 200px;
      border-right: 1px solid #edeff3;
      font-size: 30px;
      color: #777777;
    }
    .van-tab--active {
      color: #333333;
    }
    .van-tabs__nav {
      padding-bottom: 0;
    }
    .van-tabs__line {
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
    }
    .placeholder {
      //  flex-shrink 不参与空间分配，默认是 1 参与分配空间
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }
    .hanburger-btn {
      position: fixed;
      right: 0;
      width: 66px;
      height: 82px;
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: rgba(255, 255, 255, 0.902);
      i.toutiao {
        font-size: 33px;
      }
      &:before {
        content: '';
        position: absolute;
        left: 0;
        width: 2px;
        height: 58px;
        background-image: url('~@/assets/gradient-gray-line.png');
        background-size: contain;
      }
    }
  }
}
</style>
